﻿using Quartz.MUI.ClassLibrary.Model;
using HelperLibrary;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Quartz.MUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using Quartz.MUI.ClassLibrary.Tools;
using Quartz.MUI.ClassLibrary.Service;

namespace Quartz.MUI
{
    public class ThreadManager
    {
        /// <summary>
        /// 版本号
        /// </summary>
        public static string version = string.Empty;
        /// <summary>
        /// 发送时间
        /// </summary>
        public static int sendTime = 0;
        /// <summary>
        /// 启动调度数量
        /// </summary>
        public static int startCount = 0;
        /// <summary>
        /// host
        /// </summary>
        public static string host = string.Empty;

        /// <summary>
        /// 发送服务监测工具
        /// </summary>
        public static void SendToServerMonitor()
        {
            try
            {
                var ips = NetworkInterface.GetAllNetworkInterfaces()
                          .OrderByDescending(c => c.Speed)
                          .FirstOrDefault(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up);
                if (ips != null)
                {
                    string? ip = ips.GetIPProperties().UnicastAddresses
               .Where(p => p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address))
               .FirstOrDefault()?.Address.ToString();
                    if (!string.IsNullOrEmpty(host))
                    {
                        var list = HTTPHelper.Get<List<tab_quarz_task>>($"{host}?handler=SelectJob&pageindex=1&pagesize=9999");
                        startCount = list != null ? list.Count : 0;
                    }
                    SendDataInfo sendDataInfo = new()
                    {
                        ip_addr = ip ?? string.Empty,
                        source = "jds",
                        version = version,
                        startcount = startCount
                    };
                    FmMqttClient.Publish(JsonConvert.SerializeObject(sendDataInfo));
                }
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 启动任务
        /// </summary>
        public static void Start()
        {
            var timingThread = new Thread(TimingThread)
            {
                IsBackground = true
            };
            timingThread.Start();
        }

        /// <summary>
        /// 线程
        /// </summary>
        public static void TimingThread()
        {
            while (true)
            {
                try
                {
                    var ips = NetworkInterface.GetAllNetworkInterfaces()
                          .OrderByDescending(c => c.Speed)
                          .FirstOrDefault(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up);
                    if (ips != null)
                    {
                        string? ip = ips.GetIPProperties().UnicastAddresses
                   .Where(p => p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address))
                   .FirstOrDefault()?.Address.ToString();
                        if (!string.IsNullOrEmpty(host))
                        {
                            var obj = HTTPHelper.Get<SelectJobs>($"{host}?handler=SelectJob&pageindex=1&pagesize=9999");
                            startCount = obj != null ? obj.total : 0;
                        }
                        SendDataInfo sendDataInfo = new()
                        {
                            ip_addr = ip ?? string.Empty,
                            source = "jds",
                            version = version,
                            startcount = startCount
                        };
                        FmMqttClient.Publish(JsonConvert.SerializeObject(sendDataInfo));
                    }
                    Thread.CurrentThread.Join(sendTime * 60 * 1000);
                }
                catch (Exception ex)
                {
                    //LogHelper.LogErr(ex);
                }
            }
        }
    }
}
